<?php include("includes/session.php"); ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $lang['PAGE_TITLE_EDIT_PROJECT_RESOURCES']; ?></title>
<?php include('includes/categories.php'); ?>
<?php include('header.php'); ?>
<?php
if(!$session->logged_in)
{
?>
	<br />
	<center>
	<table cellpadding="0" cellspacing="0" width="70%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<center><b>You Are Not Logged In</b></center><br />
		<center>Please <a href="index.php">Log In</a> to View This Page, or <a href="register.php">Register</a> Now. </center><br />
		<center>Thank You.</center>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
<?php
}
$project = $database->getProject($_GET['id']);
if($project=="")
{
	echo "<br /><center>No Project Found</center>";
}
else
{
	$resul = $database->query("SELECT * FROM ".TBL_PROJECT_MEMBERS." WHERE projectID='".$project['id']."' AND username='".$session->username."'");
	$projectMember = mysql_fetch_array($resul);
	
	if($project['creator']==$session->username || $session->userlevel>=MANAGER_LEVEL || $projectMember['userlevel']>=PROJECT_MANAGER)
	{
?>
<table cellpadding="20" cellspacing="0" width="100%">
<tr><td>
	<center>
	<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<center><font size="+1"><b><?php echo $lang['EDIT_PROJECT_RESOURCES_NEEDED_RESOURCES']; ?></b></font></center><br />
		<?php
		if(isset($_SESSION['deleteResourceSuccess']))
		{
			if(!$_SESSION['deleteResourceSuccess'])
			{
				echo "<center><font size=\"2\" color=\"#ff0000\">Resource Deletion Failed</font></center><br />";
			}
			else
			{
				echo "<center><font size=\"2\" color=\"#00c809\">Resource Deleted Successfully</font></center><br />";
			}
			unset($_SESSION['deleteResourceSuccess']);
		}
		?>
		<?php
		if(isset($_GET['id']))
		{
			displayProjectOwnerResources($_GET['id']);
		?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
	<br />
	<center>
	<table cellpadding="0" cellspacing="0">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<?php
			if(isset($_GET['edit']))
				echo "<center><font size=\"+1\"><b>Edit Needed Resources</b></font></center><br />";
			else
				echo "<center><font size=\"+1\"><b>Add Needed Resources</b></font></center><br />";
			
			if(isset($_SESSION['addResourceSuccess']))
			{
				if(!$_SESSION['addResourceSuccess'])
				{
					echo "<center><font size=\"2\" color=\"#ff0000\">Errors: ".$form->num_errors."</font></center><br />";
				}
				else
				{
					echo "<center><font size=\"2\" color=\"#00c809\">Resource Added Successfully</font></center><br />";
				}
				unset($_SESSION['addResourceSuccess']);
			}
			else if(isset($_SESSION['editResourceSuccess']))
			{
				if(!$_SESSION['editResourceSuccess'])
				{
					echo "<center><font size=\"2\" color=\"#ff0000\">Errors: ".$form->num_errors."</font></center><br />";
				}
				else
				{
					echo "<center><font size=\"2\" color=\"#00c809\">Resource Edited Successfully</font></center><br />";
				}
				unset($_SESSION['editResourceSuccess']);
			}
			
			if(isset($_GET['edit']))
			{
				$retval = $database->query("SELECT * FROM ".TBL_NEEDS." WHERE id='".$_GET['edit']."'");
				$resource = mysql_fetch_array($retval);
		?>
		<form action="includes/process.php" method="POST">
		<input type="hidden" name="subEditNeededResource" value="<?php echo $_GET['edit']; ?>">
		<input type="hidden" name="projectID" value="<?php echo $_GET['id']; ?>">
		<table>
		<tr>
			<td align="right"><b><?php echo $lang['RESOURCES_TYPE']; ?></b></td>
			<td>
			<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
			<option value="Knowledge" <?php if($resource['type']=="Knowledge"){ echo "selected=\"selected\""; } ?>>Knowledge</option>
			<option value="Ability" <?php if($resource['type']=="Ability"){ echo "selected=\"selected\""; } ?>>Ability</option>
			<option value="Commodity" <?php if($resource['type']=="Commodity"){ echo "selected=\"selected\""; } ?>>Commodity</option>
			<option value="Transportation" <?php if($resource['type']=="Transportation"){ echo "selected=\"selected\""; } ?>>Transportation</option>
			<option value="Raw Materials" <?php if($resource['type']=="Raw Materials"){ echo "selected=\"selected\""; } ?>>Raw Materials</option>
			<option value="Land" <?php if($resource['type']=="Land"){ echo "selected=\"selected\""; } ?>>Land</option>
			</select><?php echo $form->error("type"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['RESOURCES_CATEGORY']; ?></b></td>
			<td>
			<select name="category" id="category">
			<?php 
			$result = $database->query("SELECT * FROM ".TBL_CATEGORIES." WHERE type='".$resource['type']."' ORDER BY type ASC, category ASC");
			$category = mysql_fetch_array($result);
			$num_result = mysql_num_rows($result);
			for($i=0;$i<$num_result;$i++)
			{
				$name = mysql_result($result,$i,"category");
				echo "<option value=\"$name\"";
				if($name == $resource['category'])
				{
					echo "selected=\"selected\"";
				}
				echo ">$name</option>";
			}
			?>
			</select><?php echo $form->error("category"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b>Title:</b></td>
			<td>
			<input type="text" name="title" maxlength="50" value="<?php echo $resource['title'] ?>">
			</td>
			<td><?php echo $form->error("title"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['RESOURCES_TYPE']; ?></b></td>
			<td><textarea name="description" rows="3" cols="45"><?php echo $resource['description'] ?></textarea></td>
			<td><?php echo $form->error("description"); ?></td>
		</tr>
		<tr>
			<td align="right"></td>
			<td><input type="submit" value="Save Needed Resource"> <input type="button" value="Cancel Add Resource" onClick="buttonClick('editProjectResources.php?id=<?php echo $_GET['id']; ?>');"></td>
			<td></td>
		</tr>
		</table>
		</form>
		<?
			}
			else
			{
		?>
		<form action="includes/process.php" method="POST">
		<input type="hidden" name="subAddNeededResource" value="<?php echo $_GET['id']; ?>">
		<table>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_PROJECT_RESOURCES_NEEDED_RESOURCES_TYPE']; ?></b></td>
			<td>
			<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
			<option value="">-Resource Type-</option>
			<option value="Knowledge" <?php if($form->value("type")=="Knowledge"){ echo "selected=\"selected\""; } ?>>Knowledge</option>
			<option value="Ability" <?php if($form->value("type")=="Ability"){ echo "selected=\"selected\""; } ?>>Ability</option>
			<option value="Commodity" <?php if($form->value("type")=="Commodity"){ echo "selected=\"selected\""; } ?>>Commodity</option>
			<option value="Transportation" <?php if($form->value("type")=="Transportation"){ echo "selected=\"selected\""; } ?>>Transportation</option>
			<option value="Raw Materials" <?php if($form->value("type")=="Raw Materials"){ echo "selected=\"selected\""; } ?>>Raw Materials</option>
			<option value="Land" <?php if($form->value("type")=="Land"){ echo "selected=\"selected\""; } ?>>Land</option>
			</select><?php echo $form->error("type"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_PROJECT_RESOURCES_NEEDED_RESOURCES_CATEGORY']; ?></b></td>
			<td>
			<select name="category" id="category">
			<option value="">-Category Type-</option>
			</select><?php echo $form->error("category"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_PROJECT_RESOURCES_NEEDED_RESOURCES_TITLE']; ?></b></td>
			<td>
			<input type="text" name="title" maxlength="50" value="<?php echo $form->value("title"); ?>">
			</td>
			<td><?php echo $form->error("title"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_PROJECT_RESOURCES_NEEDED_RESOURCES_DESC']; ?></b></td>
			<td><textarea name="description" rows="3" cols="45"><?php echo $form->value("description"); ?></textarea></td>
			<td><?php echo $form->error("description"); ?></td>
		</tr>
		<tr>
			<td align="right"></td>
			<td><input type="submit" value="Add Needed Resource"></td>
			<td></td>
		</tr>
		</table>
		</form>
		<?
				}
			}
		}
		?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
</td></tr>
</table>
<?php } ?>
<?php include("footer.php"); ?>